package com.hdcx.Minfresh.adapter;


import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.activity.AddAddressActivity;
import com.hdcx.Minfresh.activity.DiscountActivity;
import com.hdcx.Minfresh.activity.OrderUploadActivity;
import com.hdcx.Minfresh.bean.AdressWrapper;
import com.hdcx.Minfresh.bean.OrderDetailWrapper;
import com.hdcx.Minfresh.url.RequstCode;
import com.hdcx.Minfresh.util.AppUtil;

/**
 * Created by Administrator on 2016/11/9.
 */

public class OrderUploadAdapter extends RecyclerView.Adapter<OrderUploadAdapter.Holder> {
    OrderUploadActivity mActivity;
    OrderDetailWrapper data;
    AdressWrapper address;
    public String note;

    public OrderUploadAdapter(OrderUploadActivity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        if (viewType == 0) {
            view = layoutInflater.inflate(R.layout.item_fragment_sub_one_, parent, false);
        } else if (viewType == data.getGlist().size() + 1) {
            view = layoutInflater.inflate(R.layout.item_fragment_sub_three_, parent, false);
        } else {
            view = layoutInflater.inflate(R.layout.item_activity_collect, parent, false);
        }
        return new Holder(view, viewType);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        if (position == 0) {
            if (!mActivity.tag) {
                holder.layout_addinfo.setVisibility(View.VISIBLE);
                holder.tv_addtext.setVisibility(View.INVISIBLE);
                holder.tv_address.setText(address.getUser().getAddress());
                holder.tv_name.setText("收货人:" + address.getUser().getName());
                holder.tv_phone.setText(address.getUser().getMobile());
            } else {
                holder.layout_addinfo.setVisibility(View.INVISIBLE);
                holder.tv_addtext.setVisibility(View.VISIBLE);
                holder.layout_address.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mActivity.tag) {
                            mActivity.startActivityForResult(new Intent(mActivity, AddAddressActivity.class), RequstCode.ADDADDRESS);
                        }

                    }
                });
            }
        } else if (position == data.getGlist().size() + 1) {
            for (int i = 0; i < holder.layouts.length; i++) {
                final int finalI = i;
                holder.layouts[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int j = 0; j < holder.imgs.length; j++) {
                            mActivity.select_whitch = finalI;
                            holder.imgs[j].setImageResource(j == finalI ? R.drawable.ic_gou : R.drawable.ic_quan);
                        }
                    }
                });
            }
            holder.yue_money.setText("(" + data.getAccount() + ")");
            holder.tv_quan.setText(data.getOrder().getDiscount_value());
            Log.e("OrderUploadAdapter", "OrderUploadAdapter--onBindViewHolder--优惠券id"+data.getOrder().getDiscount_id());
            if (Integer.parseInt(data.getOrder().getDiscount_id())!=0){
                holder.layout_quan.setEnabled(false);
            }
            holder.layout_quan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent;
                    //UserInfoBean loginData = ShpfUtil.getObject(ShpfUtil.LOGIN);
                    intent = new Intent(mActivity, DiscountActivity.class);
                  /*  intent.putExtra("url", "http://120.77.23.6//api/user/getdiscount.html?uid=" + loginData.getUserinfo().getId());
                    intent.putExtra("title", "优惠券");*/
                    intent.putExtra("min", Double.parseDouble(data.getOrder().getOrder_amount()));
                    mActivity.startActivityForResult(intent, RequstCode.DIS);
                    mActivity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                }
            });
            holder.editNote.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    note = charSequence.toString();
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });
        } else {
            OrderDetailWrapper.Glist temp = data.getGlist().get(position - 1);
            AppUtil.showPic(temp.getGoods_img(), holder.img_main);
            holder.tv_name2.setText(temp.getGoods_title());
            holder.tv_intro.setText(temp.getGoods_short_title());
            holder.tv_intro_2.setText("规格:" + temp.getGoods_etalon() + temp.getGoods_units());
            holder.tv_money.setText(temp.getGoods_price() + temp.getGoods_units());
            holder.tv_units.setText(temp.getOrder_quantity() + temp.getGoods_units());
        }
    }

    @Override
    public int getItemCount() {
        if (data == null || data.getGlist() == null) {
            return 0;
        }
        return data.getGlist().size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public void setData(OrderDetailWrapper goodlist) {
        data = goodlist;
        notifyDataSetChanged();
    }

    public void setAddressData(AdressWrapper response) {
        address = response;
        notifyDataSetChanged();
    }

    public void setDisName(String name) {
        data.getOrder().setDiscount_value(name);
//        data.setDisName(name);
        notifyDataSetChanged();
    }

    public String getNote() {
        return note;
    }


    public class Holder extends RecyclerView.ViewHolder {
        RelativeLayout layout_address, layout_addinfo;
        public LinearLayout layout_quan;
        public TextView tv_name, tv_address, tv_phone, tv_intro, tv_intro_2, tv_units, tv_name2, tv_money, tv_addtext, yue_money;
        ImageView img_main;
        TextView tv_quan;
        public EditText editNote;
        public LinearLayout[] layouts;
        public ImageView[] imgs;

        public Holder(View itemView, int viewType) {
            super(itemView);
            if (viewType == 0) {
                layout_address = (RelativeLayout) itemView.findViewById(R.id.layout_address);
                layout_addinfo = (RelativeLayout) itemView.findViewById(R.id.layout_addinfo);
                tv_name = (TextView) itemView.findViewById(R.id.tv_name);
                tv_addtext = (TextView) itemView.findViewById(R.id.tv_addtext);
                tv_address = (TextView) itemView.findViewById(R.id.tv_address);
                tv_phone = (TextView) itemView.findViewById(R.id.tv_phone);
            } else if (viewType == data.getGlist().size() + 1) {
                layouts = new LinearLayout[]{(LinearLayout) itemView.findViewById(R.id.layout_weixin), (LinearLayout) itemView.findViewById(R.id.layout_zhifubao), (LinearLayout) itemView.findViewById(R.id.layout_yue)};
                imgs = new ImageView[]{(ImageView) itemView.findViewById(R.id.img_weixin), (ImageView) itemView.findViewById(R.id.img_zhifubao), (ImageView) itemView.findViewById(R.id.img_yue)};
                layout_quan = (LinearLayout) itemView.findViewById(R.id.layout_quan);
                tv_quan = (TextView) itemView.findViewById(R.id.tv_quan);
                yue_money = (TextView) itemView.findViewById(R.id.tv_money);
                editNote = (EditText) itemView.findViewById(R.id.editNote);
            } else {
                tv_intro = (TextView) itemView.findViewById(R.id.tv_intro);
                tv_intro_2 = (TextView) itemView.findViewById(R.id.tv_intro_2);
                tv_units = (TextView) itemView.findViewById(R.id.tv_units);
                tv_name2 = (TextView) itemView.findViewById(R.id.tv_name);
                tv_money = (TextView) itemView.findViewById(R.id.tv_money);
                img_main = (ImageView) itemView.findViewById(R.id.img_main);
            }
        }
    }
}
